<?php
class VideosController extends AppController {

	var $name = 'Videos';
	var $paginate = array(
		'oder' => array(
			'Videos.id'	=>	'desc'),
		'limit' => '5'
	);
	function index() {
		//$this->layout = 'youtube';
		$this->Video->recursive = 0;
		$this->set('videos', $this->paginate());
		//home_mediabars

	}
	function show() {
		$this->layout = 'youtube';
		$this->set('home_mediabars' , $this->get_cat_limit_data(19,18));
	}
	
	
	function view($id = null) {
		$this->layout = 'youtube';
		
		if (!$id) {
			$this->Session->setFlash(__('Invalid video', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('video_info', $this->Video->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Video->create();
			$this->data['Video']['user_id'] = $_SESSION['Auth']['User']['id'];
			if ($this->Video->save($this->data)) {
				$this->Session->setFlash(__('The video has been saved', true));
				$this->redirect(array('action' => 'add'));
			} else {
				$this->Session->setFlash(__('The video could not be saved. Please, try again.', true));
			}
		}
		$catvideos = $this->Video->Catvideo->find('list');
		$users = $this->Video->User->find('list');
		$this->set(compact('catvideos', 'users'));
	}
	function add_video() {

	}
	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid video', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Video->save($this->data)) {
				$this->Session->setFlash(__('The video has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The video could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Video->read(null, $id);
		}
		$catvideos = $this->Video->Catvideo->find('list');
		$users = $this->Video->User->find('list');
		$this->set(compact('catvideos', 'users'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for video', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Video->delete($id)) {
			$this->Session->setFlash(__('Video deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Video was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
	
	function find() {

	}
	
	function youtube_ajax_autoload(){
		Configure::write('debug',0);
		if($this->RequestHandler->isAjax()){
			$video_id = null;
			$keyword ='';
			if(isset($_POST['set_txt_search']))
				$keyword = $_POST['set_txt_search'];
		/*	if(isset($_POST['set_select_search']))
				$type_search = $_POST['set_select_search'];
				
				*/
			$this->set('keyword',$keyword);
		}
		else{
			$this->autoRender = false;
		}
	}
	
	function youtube_ajax_search(){
		Configure::write('debug',0);
		if($this->RequestHandler->isAjax()){
			$video_id = null;
			$keyword ='';
			if(isset($_POST['set_txt_search']))
				$keyword = $_POST['set_txt_search'];
			$type_search = '';
			if(isset($_POST['set_select_search']))
				$type_search = $_POST['set_select_search'];
	
			$this->set('keyword',$keyword);
			$this->set('type_search',$type_search);
		}
		else{
			$this->autoRender = false;
		}
	}
	
	function youtube_ajax_search_add_multi(){
		Configure::write('debug',0);
		if($this->RequestHandler->isAjax()){
			$video_id = null;
			$keyword ='';
			if(isset($_POST['set_txt_search']))
				$keyword = $_POST['set_txt_search'];
			$type_search = '';
			if(isset($_POST['set_select_search']))
				$type_search = $_POST['set_select_search'];
	
			$this->set('keyword',$keyword);
			$this->set('type_search',$type_search);
		}
		else{
			$this->autoRender = false;
		}
	}
	
	function ajax_admin_youtube_ajax_search() {
		Configure::write('debug',0);
		if($this->RequestHandler->isAjax()){
			$video_id = null;
			$keyword ='';
			if(isset($_POST['set_txt_search']))
				$keyword = $_POST['set_txt_search'];
			$type_search = '';
			if(isset($_POST['set_select_search']))
				$type_search = $_POST['set_select_search'];
	
			$this->set('keyword',$keyword);
			$this->set('type_search',$type_search);
		}
		else{
			$this->autoRender = false;
		}
	}


}
